//深拷贝
export function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach((keys) => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}
// Markdown转html文本
export function parsedMarkdown(text) {
  const lines = text.split('\n')
  const arr = []
  lines.forEach((line, index) => {
    if (line.startsWith('#')) {
      const level = line.replace(/#/g, '')
      arr.push(`<h${level.length}>${line.replace(new RegExp(`^#+`, 'g'), '')}</h${level.length}>`)
    } else if (line.startsWith('##')) {
      arr.push('<h2>' + line.replace(new RegExp(`^##+`, 'g'), '') + '</h2>')
    } else if (line.startsWith('###')) {
      arr.push('<h3>' + line.replace(new RegExp(`^###+`, 'g'), '') + '</h3>')
    } else if (line.startsWith('####')) {
      arr.push('<h4>' + line.replace(new RegExp(`^####+`, 'g'), '') + '</h4>')
    } else if (line.startsWith('#####')) {
      arr.push('<h5>' + line.replace(new RegExp(`^#####+`, 'g'), '') + '</h5>')
    } else if (line.startsWith('######')) {
      arr.push('<h6>' + line.replace(new RegExp(`^######+`, 'g'), '') + '</h6>')
    } else if (line.startsWith('*') || line.startsWith('_')) {
      arr.push('<p >' + line.replace(/^_*/, '').replace(/\*|_/g, '<p ></p>').trim() + '</p>')
    } else if (line.startsWith('>')) {
      arr.push('<blockquote>' + line.replace(/^> /, '').trim() + '</blockquote>')
    } else if (line.startsWith('-')) {
      arr.push('<li>' + line.replace(/^-/g, '').trim() + '</li>')
    } else {
      arr.push('<p >' + line.trim() + '</p>')
    }
  })

  return arr.join('')
}
//   下载文件
export function downloadFile(data, fileName) {
  let blob = new Blob([data])
  let downloadElement = document.createElement('a')
  let href = window.URL.createObjectURL(blob)
  downloadElement.href = href
  downloadElement.download = fileName
  document.body.appendChild(downloadElement)
  downloadElement.click()
  document.body.removeChild(downloadElement)
  window.URL.revokeObjectURL(href)
}

//秒转hh:mm:ss
export function convertToTime(duration) {
  // 将总秒数转换为整数
  const totalSeconds = Math.floor(duration)
  // 计算小时数
  const hours = Math.floor(totalSeconds / 3600)
  // 计算剩余的秒数
  const remainingSeconds = totalSeconds % 3600
  // 计算分钟数
  const minutes = Math.floor(remainingSeconds / 60)
  // 计算最终的秒数
  const seconds = remainingSeconds % 60
  // 格式化输出，确保每个部分都是两位数
  const formattedHours = String(hours).padStart(2, '0')
  const formattedMinutes = String(minutes).padStart(2, '0')
  const formattedSeconds = String(seconds).padStart(2, '0')
  return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`
}
